// pages/userInfo/userInfo.js
Page({
  data: {
    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',  //微信默认的灰色头像
    nickName: '',
    nicknameLength: 0,
  },

  // 获取用户信息回调
  getAvatar(e) {
    console.log(e)
    if (e.detail) {
      this.setData({
        avatarUrl: e.detail.avatarUrl
      })
    }
  },
  onBackPress(res) {
    // 返回 false 阻止默认返回行为
    return false;
  },
  // 昵称输入处理
  onNicknameInput(e) {
    const value = e.detail.value
    const length = value.replace(/[\u4e00-\u9fa5]/g, 'aa').length
    this.setData({
      nickName: value,
      nicknameLength: length
    })
  },

  // 提交信息
  onSubmit() {
    const userId = wx.getStorageSync('userId')
    if (!this.data.nickName) {
      wx.showToast({ title: '请输入昵称', icon: 'none' })
      return
    }

    if (this.data.nicknameLength < 2 || this.data.nicknameLength > 32) {
      wx.showToast({ title: '昵称长度为2-32个字符', icon: 'none' })
      return
    }
    wx.request({
      url: 'http://a1.iwmls.com/index/login/renzheng',
      method: 'POST',
      data: {
          id: userId,
          nickname: this.data.nickName,
          avatarurl: this.data.avatarUrl
      },success: (res) => {
          console.log(res)
      },
      fail: (res) => {
        console.log(res)
      }
    })
    wx.showToast({ 
      title: '提交成功' ,
      duration: 2000
    })
    setTimeout(() => {
      wx.navigateBack()
    }, 2000);
  
  },
})